/*!
 * Copyright (c) 2023-present StarDreamNet
 */
(document.loader || (document.loader = [])).push(["banner", class {
    constructor() {
        this._drag = {
            ing: false,
            x: 0,
            page: 0,
            np: 0,
            di: false
        };
        this._urls = [];
    }

    /**
     * @param {number} value
     */
    set page(value) { // 设置当前页
        value = Math.max(0, Math.min(value, this.len - 1));
        this._page = value;
        this.slider(-(Math.floor(value)));
        return void 0;
    };
    get page() { // 获取当前页
        return this._page;
    };
    get len() { // 页数
        return $(".app-main-begin-banner-image>li").length;
    };
    get width() { // 显示区域长度
        return $(".app-main-begin-banner").width();
    };

    /**
     * @param {{title:string,img:string,url:string}} data
     */
    addPage(data) { // 添加页
        this._urls.push(data.url);
        var e = $("<li></li>");
        e.addClass("app-main-begin-banner-image-item");
        e.attr("title", data.title);
        var im = $("<img>");
        im.attr("src", data.img);
        im.attr("alt", data.title);
        im.attr("draggable", "false");
        im.prop("url", data.url);
        im.appendTo(e);
        e.appendTo($(".app-main-begin-banner-image"));
        var p = $("<li></li>");
        p.appendTo($(".app-main-begin-banner-pages"));
    };

    /**
     * @param {number} num
     */
    slider(num) { // 滑动到某一页
        $(".app-main-begin-banner-pages>li").each((i, v) => {
            var n = i - Math.abs(num);
            if (n > -0.5 && n <= 0.5) {
                $(v).attr("select", "");
            } else {
                $(v).removeAttr("select");
            }
        });
        $(".app-main-begin-banner-image>li").each((i, v) => {
            $(v).css("transform", "translateX(" + String((i + num) * this.width) + "px)");
        })
    };

    next() { // 下一页
        if (this.page == this.len - 1) {
            this.page = 0;
        } else {
            this.page++;
        }
    };

    back() { // 上一页
        if (this.page == 0) {
            this.page = this.len - 1;
        } else {
            this.page--;
        }
    };

    initButtons() { // 初始化按钮
        $(".app-main-begin-banner-pages>li").each((i, v) => {
            $(v).on("click", () => {
                this.page = i;
            })
        });
        $(".app-main-begin-banner-buttons-left").on("click", () => {
            this.back()
        })
        $(".app-main-begin-banner-buttons-right").on("click", () => {
            this.next()
        })
    };

    slideStart(x, v) { // 滑动开始
        if (!this._drag.ing) {
            v.addClass("dragging");
            this._drag.ing = true;
            this._drag.di = false;
            this._drag.x = x;
            this._drag.page = Number(-this.page);
        }
    };

    slideEnd(v) { // 滑动结束
        if (this._drag.ing) {
            v.removeClass("dragging");
            if (!this._drag.di) {
                let url = this._urls[this.page];
                if (url) window.open(url);
            } else {
                this.page = Math.round(-this._drag.np);
            }
            this._drag.ing = false;
            this._drag.np = 0;
            this._drag.x = 0;
        }
    };

    slideMove(x) { // 滑动中
        if (this._drag.ing) {
            this._drag.di = true;
            this._drag.np = this._drag.page + 1 / this.width * (x - this._drag.x)
            this.slider(this._drag.np);
        }
    };

    initSlide() { // 初始化滑动
        var v = $(".app-main-begin-banner-image")
        v.on("mousedown", ({ clientX }) => {
            this.slideStart(clientX, v)
        });
        v.on("touchstart", ({ touches }) => {
            this.slideStart(touches[0].clientX, v)
        });
        v.on("mousemove", ({ clientX }) => {
            this.slideMove(clientX)
        });
        v.on("touchmove", ({ touches }) => {
            this.slideMove(touches[0].clientX)
        });
        v.on("mouseup", () => {
            this.slideEnd(v)
        });
        v.on("mouseout", () => {
            this.slideEnd(v)
        });
        v.on("touchend", () => {
            this.slideEnd(v)
        });
    };

    initAutoSilde() { // 初始化自动滑动
        var v = $(".app-main-begin-banner");
        var v1 = $(".app-main-begin-banner-image");

        v.on("mouseenter", () => {
            v.addClass("reading");
        });
        v.on("mouseout", () => {
            v.removeClass("reading");
        });

        this.startAutoSilde(v, v1)
    };

    startAutoSilde(v, v1) { // 开始自动滑动
        var t = 0;
        setInterval(() => {
            if (v.hasClass("reading") || v1.hasClass("dragging")) return;
            t++;
            if (t > 300) {
                this.next();
                t = 0;
            }
        }, 1000 / 60)
    }

    init_(require) {
        $.ajax({
            url: "/api/get_banner?t=" + Date.now(),
            contentType: 'application/json',
        }).done((response) => {
            if (response.code == 200) { // 状况码为200
                response.data.forEach(d => { // 遍历响应内容添加页面
                    this.addPage(d);
                });

                this.page = 0;
                this.initButtons();
                this.initSlide();
                this.initAutoSilde();
            } else {
                $(".app-main-begin-banner").text(response.nsg || '加载失败');
            }

        }).fail(() => {
            $(".app-main-begin-banner").text("加载失败");
        });
    }
}])